package cn.interestingshop.service.goods;
import java.sql.Connection;
import java.util.List;

import cn.interestingshop.mapper.goods.GoodsMapper;
import cn.interestingshop.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

import cn.interestingshop.dao.goods.GoodsDao;
import cn.interestingshop.dao.goods.GoodsDaoImpl;
import cn.interestingshop.entity.Goods;
import cn.interestingshop.utils.DataSourceUtil;

/**
 * 商品的业务类
 */
public class GoodsServiceImpl implements GoodsService {
	
	private Logger logger = Logger.getLogger(GoodsServiceImpl.class);
	
	@Override
	public boolean save(Goods goods) {
		SqlSession session = null;
		Boolean flag = false;
		try {
			session = MyBatisUtil.createSqlSession();
			int count = session.getMapper(GoodsMapper.class).save(goods);
			if (count > 0) {
				flag = true;
				session.commit();
			}
		} catch (Exception e) {
			e.printStackTrace();
			session.rollback();
		} finally {
			MyBatisUtil.closeSqlSession(session);
		}
		return flag;
	}

	@Override
	public boolean update(Goods goods) {
		SqlSession session = null;
		Boolean flag = false;
		try {
			session = MyBatisUtil.createSqlSession();
			int count = session.getMapper(GoodsMapper.class).update(goods);
			if (count > 0) {
				flag = true;
				session.commit();
			}
		} catch (Exception e) {
			e.printStackTrace();
			session.rollback();
		} finally {
			MyBatisUtil.closeSqlSession(session);
		}
		return flag;
	}

	@Override
	public boolean deleteById(Integer goodsId) {
		SqlSession session = null;
		Boolean flag = false;
		try {
			session = MyBatisUtil.createSqlSession();
			int count = session.getMapper(GoodsMapper.class).deleteById(goodsId);
			if (count > 0) {
				flag = true;
				session.commit();
			}
		} catch (Exception e) {
			e.printStackTrace();
			session.rollback();
		} finally {
			MyBatisUtil.closeSqlSession(session);
		}
		return flag;
	}

	@Override
	public Goods getById(Integer goodsId) {
		SqlSession session = null;
		try {
			session= MyBatisUtil.createSqlSession();
			return session.getMapper(GoodsMapper.class).selectById(goodsId);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyBatisUtil.closeSqlSession(session);
		}
		return null;
	}

	@Override
	public List<Goods> getList(Integer currentPageNo,Integer pageSize,String goodsName, Integer categoryId) {
		SqlSession session = null;
		try {
			session=MyBatisUtil.createSqlSession();
			return session.getMapper(GoodsMapper.class).selectList(currentPageNo,pageSize,goodsName,categoryId);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyBatisUtil.closeSqlSession(session);
		}
		return null;
	}

	@Override
	public int getCount(String goodsName,Integer categoryId) {
		SqlSession session = null;
		try {
			session=MyBatisUtil.createSqlSession();
			return session.getMapper(GoodsMapper.class).selectCount(goodsName,categoryId);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyBatisUtil.closeSqlSession(session);
		}
		return 0;
	}

	@Override
	public boolean updateStock(Integer goodsId, Integer stock) {
		SqlSession session = null;
		Boolean flag = false;
		try {
			session=MyBatisUtil.createSqlSession();
			int count = session.getMapper(GoodsMapper.class).updateStock(goodsId,stock);
			if (count > 0) {
				flag = true;
				session.commit();
			}
		} catch (Exception e) {
			e.printStackTrace();
			session.rollback();
		} finally {
			MyBatisUtil.closeSqlSession(session);
		}
		return flag;
	}
}
